<extend name="public:base"/>

<block name="main">

    <div class="slide_main fr">
        <h1 class="frame_title mb10">站内信</h1>
        <div class="frame_slide_con">
            <!-- 站内信 -->
            <table id="letterTable" width="100%" cellspacing="0" cellpadding="0" class="tableBlue letterTable">
                <tbody>
                <tr>
                    <td colspan="5" class="a999" style="text-align: left;">
                        未读<span class="af60" id="spSysMsgCountUn"> {$unReadCount|default='0'} </span> 封，
                        已读<span class="af60" id="spSysMsgCountRead"> {$readCount|default='0'} </span> 封，
                        共 <span class="af60" id="spSysMsgCountTotal"> {$totalCount|default='0'} </span> 封
                    </td>
                </tr>

                <tr>
                    <th class="first"></th>
                    <th><img src="__IMG__/read.jpg"/></th>
                    <th>发件人</th>
                    <th>主题</th>
                    <th class="last">时间</th>
                </tr>

                <notempty name="list">
                    <volist name="list" id="v">

                        <tr class="listTr"  <if condition="$v['status'] eq 0">data-id="{$v.id}"</if>>
                        <td class="first">
                            <input class="selectone" type="checkbox" name="checkmsg" value="{$v.id}" />
                        </td>
                        <td>
                            <if condition="$v['status'] eq 0">
                                <img src="__IMG__/unread.jpg"/>
                            <else/>
                                <img src="__IMG__/read.jpg"/>
                            </if>
                        </td>
                        <td class="from">系统GM</td>
                        <td class="subject <if condition="$v['status'] neq 0"> read </if>">{$v.title}</td>
                        <td class="last time">{$v.create_time}</td>
                    </tr>
                    <tr class="contentTr">
                        <td colspan="5">{$v.info}</td>
                        </tr>
                    </volist>

                <else/>
                    <td colspan="100" class="text-center"> 暂无内容</td>
                </notempty>

                <tr>
                    <td colspan="5" class="first">
                        <input class="selectall" id="selectall" type="checkbox" />
                        <label for="selectall" class="mr20">全选</label>
                        <a href="javascript:void(0);" id="deletebtn" class="af60 un mr20">删除</a>
                        <a href="javascript:void(0);" id="allreadbtn" class="agreen un">已读</a>
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="page mt20">
                {$page|default=''}
            </div>

        </div>
    </div>

</block>

<block name="js">

    <script type="text/javascript" src="__JS__/layer/layer.js"></script>
    <script type="text/javascript">
        $(function () {

            // 站内信点击显示内容
            $("#letterTable .subject").click(function () {

                var self = $(this);
                var selfTr = self.parents(".listTr");
                var msgId = selfTr.attr('data-id');  //信件 ID
                var imgUrl = '__IMG__/read.jpg';  //已读信img的URL

                self.addClass('read');
                if (selfTr.next('.contentTr').is(".reading")) {
                    selfTr.siblings('.contentTr').removeClass('reading');
                } else {
                    selfTr.siblings('.contentTr').removeClass('reading');
                    selfTr.next('.contentTr').addClass('reading');
                }
                if(isNaN(msgId)){
                    return false;
                }else {
                    selfTr.find('img').attr('src', imgUrl);
                    $.ajax({
                        type: 'POST',
                        url: "{:url('user/msg/read')}",
                        data: {'ids': msgId},
                        dataType: 'json',
                        success: function (data) {
                            if (data.code == 1) {
                                selfTr.removeAttr('data-id');
                                return false;
                            }
                        }
                    });
                }
            });

            // 全选
            $("#selectall").click(function () {
                $("input[name='checkmsg']").prop("checked", this.checked);
            });

            /* 批量删除 */
            $("#deletebtn").click(function () {

                // 判断是否至少选择一项
                var checkedNum = $("input[name='checkmsg']:checked").length;
                if (checkedNum == 0) {
                    layer.alert('请先选择要删除的站内信！', {
                        icon: 0
                    });
                    return false;
                }

                // 批量选择
                var checkedList = [];
                $("input[name=checkmsg]:checked").each(function () {
                    checkedList.push($(this).val());
                });

                $.ajax({
                    type: "POST",
                    url: "{:url('user/msg/del')}",
                    data: {'ids': checkedList.toString()},
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == 1) {
                            layer.alert(data.msg, {icon: 1},function(index){
                                layer.close(index);
                                window.location.reload();
                            });

                        } else {
                            layer.alert(data.msg,{icon: 2});
                            return false;
                        }
                    }
                });
            });

            // 批量已读
            $("#allreadbtn").click(function () {

                // 判断是否至少选择一项
                var checkedNum = $("input[name=checkmsg]:checked").length;
                if (checkedNum == 0) {
                    layer.alert('请先选择要标记已读的站内信！', {
                        icon: 0
                    });
                    return false;
                }

                // 批量选择
                var checkedList = [];
                $("input[name=checkmsg]:checked").each(function () {
                    checkedList.push($(this).val());
                });
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: "{:url('user/msg/read')}",
                    data: {'ids': checkedList.toString()},
                    success: function (data) {
                        if (data.code == 1) {
                            layer.alert(data.msg, {
                                icon: 1
                            });
                            setTimeout(function(){
                                window.location.reload();
                            },1500);
                        } else {
                            layer.alert(data.msg, {
                                icon: 2
                            });
                            return false;
                        }
                    }
                });
            });

        });
    </script>
</block>

